;-------------------------------------------------------------LICENSE--------------------------------------------------------------;
;                                                                                                                                  ;
;The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    ;
;and Message Passing Interface (MPI) parallelization.                                                                              ;
;                                                                                                                                  ;
;Copyright (C) 2012                                                                                                                ;
;Ronglin Jiang                                                                                                                     ;
;rljiang@ssc.net.cn                                                                                                                ;
;585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             ;
;                                                                                                                                  ;
;This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         ;
;as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             ;
;                                                                                                                                  ;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    ;
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        ;
;                                                                                                                                  ;
;You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     ;
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   ;
;                                                                                                                                  ;
;-------------------------------------------------------------LICENSE--------------------------------------------------------------;

;======================================================================================================================|
function name_seq,name,format,ignore_first=ignore_first,ignore_last=ignore_last,ignore_suffix=ignore_suffix,  $
  nosymbol=nosymbol,padding=padding
;======================================================================================================================|
; Purpose
;    Name a file as a member of sequence (01.dat, 02.dat,..., 10.dat, 15.dat).
;
; Syntax
;    result=name_seq(name, format [,ignore_last=ignore_last [,ignore_last=ignore_last] [, /ignore_suffix] [, /nosymbol]).
;
; Inputs
;    name: the original name.
;    format: the format of output name, e.g. '3_1', '2.1', which consist of a number character and a symbol and a number character.
;    ignore_first: ignore the first several characters.
;    ignore_last: ignore the last several characters.
;    ignore_suffix: set this keyword to ignore the suffix of the name.
;    nosymbol: set this keyword to ignore to show the symbol of format in the output of the name
;    padding: for the symbol to add to the front and end of the name if not enough character. The default is '0'.
;
; Outputs
;    Formatted name
; 
; Example
;    print,name_seq('1','3.2')
;    IDL prints:
;    001.00
;    
;    print,name_seq('1','3.0')
;    IDL prints:
;    001.
;    
;    print,name_seq('1','3.0',/nosymbol)
;    IDL prints:
;    001
;    
;    print,name_seq('123.456','5.2')
;    IDL prints:
;    00123.45
;    
;    print,name_seq('123.png','5.0',/nosymbol,/ignore_suffix)
;    IDL prints:
;    00123.png
;    
;    print,name_seq('123abc.png','5.0',/nosymbol,/ignore_suffix,ignore_last=3)
;    IDL prints:
;    00123abc.png
;    
;    print,name_seq('year123day.png','5.0',/nosymbol,/ignore_suffix,ignore_last=3,ignore_first=4)
;    IDL prints:
;    year00123day.png
;      
;    print,name_seq('123','5.0',/nosymbol,padding='&')
;    IDL prints:
;    &&123
;    
; History
;    2010-07-15 written by R. L. Jiang at Kwasan Observatory.
;======================================================================================================================|

on_error,2

if n_elements(name) eq 0 then message,' The name is not specified.' 
if n_elements(format) eq 0 then message,' The format of the ouput name is not specified.' 
if strlen(format) lt 3 then message,' The format of the ouput name is not correct.' 
if not keyword_set(padding) then padding='0'

name_len=strlen(name)

if keyword_set(ignore_suffix) then begin
  suffix_pos=strpos(name,'.',/reverse_search)
  if suffix_pos eq -1 then begin
    suffix='' 
  endif else begin
    suffix=strmid(name,suffix_pos)
    suffix_len=strlen(suffix)
    name=strmid(name,0,name_len-suffix_len)
  endelse
endif else suffix=''

if keyword_set(ignore_first) then begin
  name_first=strmid(name,0,ignore_first)
  name=strmid(name,ignore_first)
endif else name_first=''

name_len=strlen(name)

if keyword_set(ignore_last) then begin
  name_last=strmid(name,ignore_last-1,/reverse_offset)
  name=strmid(name,0,name_len-ignore_last)
endif else name_last=''

name_len=strlen(name)

format_len=strlen(format)

last_bit=0
digit=0
formats=0
for leni=formats,format_len-1 do begin
  formati=strmid(format,leni,1,/reverse_offset)
  if formati ge '0' and formati le '9' then last_bit=10^digit*fix(formati)+last_bit else break
  digit=digit+1
endfor
symbol=strmid(format,leni,1,/reverse_offset)
first_bit=0
digit=0
formats=leni+1
for leni=formats,format_len-1 do begin
  formati=strmid(format,leni,1,/reverse_offset)
  if formati ge '0' and formati le '9' then first_bit=10^digit*fix(formati)+first_bit else break
  digit=digit+1
endfor

symbol_pos=strpos(name,symbol)
if symbol_pos eq -1 then symbol_pos=name_len

name_f=strmid(name,0,symbol_pos)
name_f_len=strlen(name_f)
name_b=strmid(name,symbol_pos+1)
name_b_len=strlen(name_b)

name_f_o=''
for leni=0,first_bit-1 do name_f_o=name_f_o+padding 
name_b_o=''
for leni=0,last_bit-1 do name_b_o=name_b_o+padding 

ins_pos=first_bit-name_f_len
if ins_pos lt 0 then begin
  name_f=strmid(name_f,name_f_len-first_bit)
  ins_pos=0
endif
strput,name_f_o,name_f,ins_pos
strput,name_b_o,name_b

if keyword_set(nosymbol) then symbol=''

return,name_first+name_f_o+symbol+name_b_o+name_last+suffix

end